package com.qinaxiwit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qianxi.wit.pojo.TbAdministrator;
import com.qianxi.wit.pojo.TbDiagnosticLogout;
import com.qianxi.wit.pojo.TbSufferer;
import com.qinaxiwit.mapper.TbAdministratorMapper;
import com.qinaxiwit.mapper.TbDiagnosticLogoutMapper;
import com.qinaxiwit.mapper.TbSuffererMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class TbSuffererService {

    @Autowired
    private TbSuffererMapper tbSuffererMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private TbDiagnosticLogoutMapper tbDiagnosticLogoutMapper;
    @Autowired
    private TbAdministratorMapper administratorMapper;

    public Page<TbSufferer> findAll(int pageNum, int pageSize){
        Page<TbSufferer> page = new Page<TbSufferer>(pageNum, pageSize);
        tbSuffererMapper.selectPage(page, null);
        return page;
    }

    public Page<TbDiagnosticLogout> findSufferer(int pageNum, int pageSize, long id){
        Page<TbDiagnosticLogout> page = new Page<TbDiagnosticLogout>(pageNum, pageSize);
        QueryWrapper<TbDiagnosticLogout> wrapper = new QueryWrapper<>();
        if (id !=0){
            wrapper.eq("patient_id",id);
        }
        tbDiagnosticLogoutMapper.selectPage(page, wrapper);
        return page;
    }

    public TbDiagnosticLogout findbyid(int id) {
        return tbDiagnosticLogoutMapper.selectById(id);
    }

    public TbAdministrator findByToken(TbSufferer tbSufferer) {
        QueryWrapper<TbAdministrator> wrapper = new QueryWrapper<>();
        wrapper.eq("administrator_username",tbSufferer.getPatientName());
        TbAdministrator tbAdministrator = administratorMapper.selectOne(wrapper);
        return tbAdministrator;
    }

    public void deleteByToken(TbSufferer tbSufferer) {
        TbAdministrator administrator = (TbAdministrator) redisTemplate.boundHashOps("administrator").get(tbSufferer.getPatientName());
        if(null!=administrator){
            redisTemplate.boundHashOps("administrator").delete(tbSufferer.getPatientName());
        }
    }
}
